package com.hk.core.autoconfigure.web;

import com.hk.commons.util.StringEscapeHandler;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.web.servlet.function.RequestPredicate;
import org.springframework.web.servlet.function.RequestPredicates;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.io.Serializable;
import java.util.Map;
import java.util.Objects;


/**
 * @author Kevin
 */
@Data
@Accessors(chain = true)
public class ApplicationInfo implements Serializable {

    /**
     * 是否添加 {@link LocaleChangeInterceptor} 到拦截链中
     */
    private boolean addLocaleChangeInterceptor = false;

    /**
     * 国际化 参数名
     *
     * @see LocaleChangeInterceptor#getParamName()
     */
    private String localParamName = "lang";

    /**
     * @see LocaleChangeInterceptor#isIgnoreInvalidLocale()
     */
    private boolean localIgnoreInvalidLocale = true;

    /* xssFilter  config*/
    /**
     * @see com.hk.core.web.filter.XssFilter#stringEscapeHandler
     */
    private StringEscapeHandler xssStringEscapeHandler = StringEscapeHandler.EnumStringEscapeHandler.DEFAULT;

    /**
     * @see com.hk.core.web.mvc.CustomRequestMappingHandlerMapping#requestMappingParameterName
     */
    private String requestMappingParameterName;

    /**
     * 是否添加 {@link com.hk.core.web.interceptors.GlobalPropertyInterceptor} 到拦截链中
     */
    private boolean addGlobalPropertyInterceptor = false;

    /**
     * {@link com.hk.core.web.interceptors.GlobalPropertyInterceptor} 全局属性拦截器资源拦截 pattern
     */
    private String[] globalPropertyInterceptorPatterns = {"/**"};

    /**
     * request 全局请求属性
     *
     * @see com.hk.core.web.interceptors.GlobalPropertyInterceptor#property
     */
    private Map<String, Object> property;

    /**
     * 表单重复提交配置
     */
    private boolean repeatSubmitPostOnly = true;

    private ServletInfo servletInfo;

    private ReactiveInfo reactiveInfo;

    public ServletInfo getServletInfo() {
        if (Objects.isNull(this.servletInfo)) {
            this.servletInfo = new ServletInfo();
        }
        return this.servletInfo;
    }

    public ReactiveInfo getReactiveInfo() {
        if (Objects.isNull(this.reactiveInfo)) {
            this.reactiveInfo = new ReactiveInfo();
        }
        return this.reactiveInfo;
    }

    @Data
    @Accessors(chain = true)
    public class ServletInfo {

        /**
         * @see com.hk.core.web.filter.XssFilter#requestPredicate
         */
        private RequestPredicate xssRequestPredicate = RequestPredicates.all();

        public ApplicationInfo and() {
            return ApplicationInfo.this;
        }
    }

    @Data
    @Accessors(chain = true)
    public class ReactiveInfo {

        private org.springframework.web.reactive.function.server.RequestPredicate xssRequestPredicate = org.springframework.web.reactive.function.server.RequestPredicates.all();

        public ApplicationInfo and() {
            return ApplicationInfo.this;
        }
    }
}
